package cluster

import (
	"context"
	"kms-backend/config"
	"kms-backend/utils/logs"

	"github.com/gin-gonic/gin"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Detail(r *gin.Context) {
	logs.Debug(nil, "获取集群详情")
	clusterId := r.Query("clusterId")
	returnData := config.ReturnData{}
	// 获取集群配置Secret
	clusterSecret, err := config.InClusterClientSet.CoreV1().Secrets(config.MetadataNamespace).Get(context.TODO(), clusterId, metav1.GetOptions{})
	if err != nil {
		// 获取集群信息失败
		logs.Error(nil, "获取集群详情失败: "+err.Error())
		msg := "获取集群详情失败"
		returnData.Message = msg
		returnData.Status = 400
	} else {
		logs.Info(map[string]interface{}{"集群Id": clusterId}, "查询集群详情成功")
		returnData.Status = 200
		returnData.Message = "查询集群详情成功"
		returnData.Data = make(map[string]interface{})
		// 使用一个map获取所需详情信息
		clusterConfigMap := clusterSecret.Annotations
		clusterConfigMap["kubeconfig"] = string(clusterSecret.Data["kubeconfig"])
		returnData.Data["item"] = clusterConfigMap
	}
	r.JSON(200, returnData)

}
